#perl perl_create_tped_from_phased_X.pl  /data1/bsi/bioinf_int/s106381.borawork/data_HTHGU/illumina1/MayoGAP/mayogap_emerge/raw/clean/beagle/run_beagle/beagle_mainrun/workflow/files/reference/hapmapfiles/xchr /data1/bsi/bioinf_int/s106381.borawork/data_HTHGU/illumina1/MayoGAP/mayogap_emerge/raw/clean/beagle/run_beagle/beagle_mainrun/workflow/files/reference/hapmapfiles/xchr/all_xchr
$dir = $ARGV[0];
chomp($dir);
$outdir = $ARGV[1];
chomp($outdir);
=head
open(WRBUFF1,">$outdir.tfam");
open(WRBUFF2,">$outdir.tped");
open(BUFF1,"../files/reference/hapmapfiles/hapmap3_noID.tfam");
while($line = <BUFF1>)
{
        chomp($line);
        @arr =split(" ",$line);
        $hash{$arr[1]} = $line;
}
@arc=("ASW_DUOS_X","ASW_TRIOS_X","CEU_DUOS_X","CEU_TRIOS_X","CHB_UNR","CHD_UNR","JPT_UNR","TSI_UNR","YRI_DUOS","YRI_TRIOS");
#@arc=("ASW_DUOS_X");
for($i=0;$i<@arc;$i++)
{
	$ph = $arc[$i];;
	$buff = "BUFF$i";
	open($buff,"$dir/$ph");
	$line = <$buff>;
	$line =~ s/\r//g;
	$line =~ s/\n//g;
	chomp($line);
	@array = split(" ",$line);
	shift(@array);
	shift(@array);
	$k =1;
#	open(WRBUFF1,">$dir/$ph.tfam");

#	 open(WRBUFF2,">$dir/$ph.tped");
  
	if($line =~ m/_0_A/ )
	{
		$div = 3;
	}
	else
	{
		$div =2;
	}
		for($i1=0;$i1<@array;$i1++)
		{
	        	@arr = split('_',$array[$i1]);
        		if($k %$div ==0)
        		{
                		print WRBUFF1 $hash{$arr[0]}."\n";
				#print $i1."\t".$arr[0]."\t".$hash{$arr[0]}."\n";
        		}
        		$k++;
		}

		while($line = <$buff>)
		{
			chomp($line);
			$line =~ s/\r//g;
			$line =~ s/\n//g;
			@array = split(" ",$line);
			$rsid = shift(@array);
			$pos = shift(@array);
			$m =0;
			@array_new =();
			if($div ==3)
			{
				for($c=0;$c<@array;$c++)
				{
					$array_new[$m] = $array[$c];
					$m++;
					$c++;
			
					$array_new[$m] = $array[$c];
                        		$m++;
                        		$c++;	
				}
			}
			else
			{
				for($c=0;$c<@array;$c++)
                                {
                                        $array_new[$m] = $array[$c];
					$m++;
				}
			}
			for($r=0;$r<@array_new;$r++)
			{
				$A = $array_new[$r];
				$r++;
				$B = $array_new[$r];
				if($A eq "-" || $B eq "-")
				{
					if($A eq "-")
					{
						$array_new[$r-1] = $B;
					}
					else
					{
						$array_new[$r] = $A;
					}
				}
			}	
		$line = join(" ",@array_new);
		if(exists($hash2{$rsid}))
		{
			$hash2{$rsid}= $hash2{$rsid}." ".$line;
		}
		else
		{
			$hash2{$rsid}= "23"." ".$rsid." "."0"." ".$pos." ".$line;
		}
#			print WRBUFF2 "23"." ".$rsid." "."0"." ".$pos." ".$line."\n";
		}
	
}
while (($key, $value) = each(%hash2)){
	print WRBUFF2 $value."\n";
}
 $outdir_new ="$outdir"."_new";
$outdir_new1  ="$outdir"."_new_final";
system(" ./plink/plink --tfile $outdir --keep ../files/reference/hapmapfiles/allpopulation.tfam --make-bed --out  $outdir_new");
system(" ./plink/plink  --tfile ../files/reference/hapmapfiles/allpopulation --make-bed --out ../files/reference/hapmapfiles/allpopulation");
=cut
$outdir_new1  ="$outdir"."_new_final";
system(" ./plink/plink  --bfile ../files/reference/hapmapfiles/allpopulation --merge-list ../files/reference/hapmapfiles/allfiles_x.txt --recode --transpose --out $outdir_new1"); 


